<?php
if($_SESSION['group_id']>2){redir("./");exit;}
	if($_POST['op']=='send'){
		$branch_id=$_POST['branch_id'];
		$subject=$_POST['subject'];
		$detail=$_POST['detail'];
		if(!$branch_id)$err="กรุณาเลือกผู้รับ";
		else if(!$subject)$err="กรุณากรอกหัวเรื่อง";
		else if(!$detail)$err="กรุณากรอกรายละเอียด";
		else{
			$from="info@$cfg_domain";
			
			$mailheaders  = "MIME-Version: 1.0\r\n";
			$mailheaders .= "Content-type: text/html; charset=utf-8 \r\n";
			$mailheaders.= "From: $cfg_domain <$from> \n";
			$mailheaders.= "Reply-To: <$profile[m_email]>\n\n";
					
			$msg = $detail;
			$res=mysql_query("select m_email from member where group_id=3 and branch_id='$branch_id'");
			$i=0;
			while($row=mysql_fetch_assoc($res)){
				++$i;
				if($i<50){
					if($i==1)
						$to=$row['m_email'];
					else
						$to.=','.$row['m_email'];
					
				}else{
					$to.=','.$row['m_email'];
					mail($to,$subject,$msg,$mailheaders);
					//echo $to."<br>";
					$i=0;
				}
			}
			//ชุดสุดท้าย
			if($i!=0){
				mail($to,$subject,$msg,$mailheaders);
				//echo $to."<br>";
			}
			redir("?mod=$mod&rand=".time(),5);
			die("<div id=com_msg>ส่งอีเมลล์เรียบร้อยแล้วกรุณารอสักครู่ <img src=images/load.gif></div>");
		}
	}
?>
<div class="box">
	<h2>ส่งอีเมลล์ถึงนักศึกษา</h2>
	<?php if($err)echo "<div id=error_msg>$err</div>";?>
	<div class="boxContent">
		<p>
	  <form method="post" enctype="multipart/form-data">
<table width="99%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td><strong>ส่งถึง</strong></td>
    <td>
    	<?php
		$sql="select * from department order by dep_id";
		$res=mysql_query($sql);
		echo "<select name=branch_id >";
		while($dep=mysql_fetch_assoc($res)){
			echo "<optgroup label='$dep[dep_name]'>";
			$sql="select * from branch where dep_id='$dep[dep_id]' order by branch_id";
			$res2=mysql_query($sql);
			while($branch=mysql_fetch_assoc($res2)){
				echo "<option value='$branch[branch_id]'>$branch[branch_name]</option>";
			}
			echo "</optgroup>";
		}
		echo "</select>";
		?>
    </td>
  </tr>
				<tr>
		  			<td width="180"><strong>หัวเรื่อง</strong></td>
					<td><input name="subject" type="text" id="subject" value="<?php echo $subject;?>" size="40" /></td>
	    		</tr>
				<tr>
                  <td><strong>รายละเอียด</strong></td>
				  <td>&nbsp;</td>
				</tr>
				<tr>
				  <td colspan="2" align="center">
				  <textarea name="detail"><?php echo $detail;?></textarea>
                      <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
                      <script type="text/javascript">
                        //<![CDATA[
                            CKEDITOR.replace( 'detail',{
                
                            skin            : 'kama',
                            language        : 'th',
                            height            : 400,
                            width            : 650,
                
                            toolbar :
                        [
                            ['Source','-','Save','NewPage','Preview'],
                            ['Cut','Copy','Paste','PasteText','PasteFromWord'],
                            ['Undo','Redo','-','Find','Rebeauty','-','SelectAll','RemoveFormat'],
                            '/',
                            ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
                            ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
                            ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                            ['Link','Unlink','Anchor'],
                            ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
                            '/',
                            ['Format','Font','FontSize'],
                            ['TextColor','BGColor'],
                            ['Maximize', 'ShowBlocks']
                
                        ], 
                
                            filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
                            filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?Type=Images',
                            filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?Type=Flash',
                            filebrowserUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                            filebrowserImageUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                            filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
                
                            } );
                        //]]>
                    </script>					</td>
	    		</tr>
  		</table>
			<table width="99%" border="0" cellspacing="2" cellpadding="2">
	    		<tr>
		  			<td align="center"><input type="submit" class="button" name="Submit" value="ส่งอีเมลล์" onclick="return confirm('ยืนยันการส่งอีเมลล์');" />
	  			    <input name="op" type="hidden" id="op" value="send" /></td>
	    		</tr>
	  		</table>
	  </form>
		</p>
	</div>
</div>